package com.talent.web;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.talent.pojo.Talent;


@Controller
public class WebController {
	static SqlSessionFactory sqlSessionFactory;
	public WebController() throws IOException {
	    String resource = "mybatis-config.xml";
	    InputStream inputStream = Resources.getResourceAsStream(resource);
	    sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
	}
	@RequestMapping(value="/")
	public String index() {
		return "index";
	}
	@RequestMapping(value="/list")
	public String list(ModelMap m) {
           SqlSession session = null;
		   try {
		    session=sqlSessionFactory.openSession();
			List<Talent> list =session.selectList("listTalent");
	        session.commit();
	        m.addAttribute("list", list);
			} catch (Exception e) {
			 e.printStackTrace();
			 session.rollback();
			} finally { 
			 session.close();
			}
		return "list";
	}
	@RequestMapping(value="/add")
	public String add() {
		return "add";
	}
	@RequestMapping(value="/addData",method = RequestMethod.POST)
	@ResponseBody
	public String addData(Talent t) {
                SqlSession session = null;	
		        try {
			    session=sqlSessionFactory.openSession();
		        Integer result = session.insert("addTalent",t);
		        session.commit();
		        if(result.equals(1)) return "success";
				} catch (Exception e) {
				 e.printStackTrace();
				 session.rollback();
				} finally { 
				 session.close();
				}
          return "fail";
	}
	@RequestMapping(value="/edit")
	public String edit(int id,ModelMap m) {
            SqlSession session = null;			
		    try {
		    session=sqlSessionFactory.openSession();
	        Talent edit = session.selectOne("getTalent", id);
	        session.commit();
	        m.addAttribute("edit",edit);
			} catch (Exception e) {
			 e.printStackTrace();
			 session.rollback();
			} finally { 
			 session.close();
			}    
		return "edit";
	}
	@RequestMapping(value="/updata",method = RequestMethod.POST)
	@ResponseBody
	public String updata(Talent t) {
            SqlSession session = null;		
		    try {
		    session=sqlSessionFactory.openSession();
		    Integer result = session.update("updateTalent",t);
	        session.commit();
	        if(result.equals(1)) return "success";  
			} catch (Exception e) {
			 e.printStackTrace();
			 session.rollback();
			} finally { 
			 session.close();
			}    
        return "fail";
	}
	@RequestMapping(value="/delete",method = RequestMethod.POST)
	@ResponseBody
	public String delete(@RequestParam(value = "id[]") Integer[] id) {
                SqlSession session = null;		
		        try {
			    session=sqlSessionFactory.openSession();
			    Talent t = new Talent();
				for (Integer i : id) {
			        t.setId(i);
			        Integer result = session.delete("deleteTalent", t);
					if(result.equals(0)) return "fail";
				}
		        session.commit();
				} catch (Exception e) {
				 e.printStackTrace();
				 session.rollback();
				} finally { 
				 session.close();
				}
	    return "success";
	}

}
